package cn.edu.zafu.bigdata.yt;

import java.sql.*;
import java.util.Scanner;

class Student {
    private String id;
    private String name;
    private String phone;

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "ID: " + id + ", Name: " + name + ", Phone: " + phone;
    }
}

public class StudentInfoSystem_yt {
    private static final String DB_URL = "jdbc:mysql://192.168.169.76:3306/learnjava";
    private static final String DB_USER = "learn";
    private static final String DB_PASSWORD = "learnpassword";

    private Connection connect() throws SQLException {
        return DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
    }

    public void addStudent(String id, String name, String phone) {
        String query = "INSERT INTO students (id, name, phone) VALUES (?, ?, ?)";
        try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(query)) {
            pstmt.setString(1, id);
            pstmt.setString(2, name);
            pstmt.setString(3, phone);
            pstmt.executeUpdate();
            System.out.println("Student added successfully.");
        } catch (SQLIntegrityConstraintViolationException e) {
            System.out.println("Student ID already exists. Please try again.");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void retrieveStudent(String id) {
        String query = "SELECT * FROM students WHERE id = ?";
        try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(query)) {
            pstmt.setString(1, id);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    Student student = new Student(rs.getString("id"), rs.getString("name"), rs.getString("phone"));
                    System.out.println(student);
                } else {
                    System.out.println("Student not found.");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void updateStudent(String id, String name, String phone) {
        String query = "UPDATE students SET name = ?, phone = ? WHERE id = ?";
        try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(query)) {
            pstmt.setString(1, name);
            pstmt.setString(2, phone);
            pstmt.setString(3, id);
            int rows = pstmt.executeUpdate();
            if (rows > 0) {
                System.out.println("Student updated successfully.");
            } else {
                System.out.println("Student not found.");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void deleteStudent(String id) {
        String query = "DELETE FROM students WHERE id = ?";
        try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(query)) {
            pstmt.setString(1, id);
            int rows = pstmt.executeUpdate();
            if (rows > 0) {
                System.out.println("Student deleted successfully.");
            } else {
                System.out.println("Student not found.");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        StudentInfoSystem_yt system = new StudentInfoSystem_yt();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume the newline character

            switch (choice) {
                case 1:
                    System.out.print("Enter ID: ");
                    String id = scanner.nextLine();
                    System.out.print("Enter Name: ");
                    String name = scanner.nextLine();
                    System.out.print("Enter Phone: ");
                    String phone = scanner.nextLine();
                    system.addStudent(id, name, phone);
                    break;
                case 2:
                    System.out.print("Enter ID to retrieve (or type 'QUIT' to go back): ");
                    while (true) {
                        String retrieveId = scanner.nextLine();
                        if (retrieveId.equalsIgnoreCase("QUIT")) break;
                        system.retrieveStudent(retrieveId);
                        System.out.print("Enter ID to retrieve (or type 'QUIT' to go back): ");
                    }
                    break;
                case 3:
                    System.out.print("Enter ID to update (or type 'QUIT' to go back): ");
                    while (true) {
                        String updateId = scanner.nextLine();
                        if (updateId.equalsIgnoreCase("QUIT")) break;
                        System.out.print("Enter New Name: ");
                        String newName = scanner.nextLine();
                        System.out.print("Enter New Phone: ");
                        String newPhone = scanner.nextLine();
                        system.updateStudent(updateId, newName, newPhone);
                        System.out.print("Enter ID to update (or type 'QUIT' to go back): ");
                    }
                    break;
                case 4:
                    System.out.print("Enter ID to delete (or type 'QUIT' to go back): ");
                    while (true) {
                        String deleteId = scanner.nextLine();
                        if (deleteId.equalsIgnoreCase("QUIT")) break;
                        system.deleteStudent(deleteId);
                        System.out.print("Enter ID to delete (or type 'QUIT' to go back): ");
                    }
                    break;
                case 5:
                    System.out.println("Exiting program. Goodbye!");
                    scanner.close();
                    return;
                default:
                    System.out.println("Invalid choice. Please try again.");
            }
        }
    }
}
